//
//  RunTimeViewController.m
//  ReviewDemo
//
//  Created by 龙 on 2018/3/14.
//  Copyright © 2018年 long. All rights reserved.
//

#import "RunTimeViewController.h"

#import "PeopleModel.h"
#import "DifferentKeyModel.h"

#import "PeopleModel+Extend.h"
#import "UIAlertView+Extend.h"
#import "UIButton+Extend.h"

#import "NSObject+ModelJson.h"
#import "NSObject+NSLog.h"
#import "MJExtension.h"

#import <objc/runtime.h>
#import <objc/message.h>


@interface RunTimeViewController ()

@end

@implementation RunTimeViewController

#pragma mark - life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor redColor];
    button.frame = CGRectMake(0, 0, 100, 35);
    button.center = self.view.center;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:@"execute" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(setUpObjc_msgSend) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.isIgnore  = NO;
    button.timeInterval = 5;//连击间隔
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.backgroundColor = [UIColor yellowColor];
    button1.frame = CGRectMake(0, 0, 100, 35);
    button1.center = CGPointMake(button.center.x, button.center.y + 60);
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 setTitle:@"alertView" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(showAlertView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.backgroundColor = [UIColor greenColor];
    button2.frame = CGRectMake(0, 0, 100, 35);
    button2.center = CGPointMake(button.center.x, button.center.y - 60);
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 setTitle:@"字典转模型" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(modelJsonMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private

- (void)showAlertView
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题" message:@"提示语" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"one",@"two", nil];
    [alertView showAlertWithHandler:^(NSInteger index) {
        NSLog(@"alertView ------ %ld",index);
    }];
}

- (void)modelJsonMethod
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PeopleModel" ofType:@"geojson"];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableContainers error:nil];
    
    
    PeopleModel *model = [PeopleModel modelWithParserDict:dict];
//    model = [PeopleModel mj_objectWithKeyValues:dict];
    NSLog(@"dict -------- %@",dict);
    
    unsigned int count = 0;//成员变量的个数
    Ivar *ivarLsit = class_copyIvarList(model.class, &count);
    for (int i = 0; i < count; i ++)
    {
        Ivar ivar1 = ivarLsit[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar1)];
        if ([model valueForKey:key])
        {
            NSLog(@"%@ == %@",key,[model valueForKey:key]);
        }
    }
    free(ivarLsit);
}

- (void)differentKeyModelMethod
{
    NSDictionary *dict1 = @{@"name1": @"张三", @"status1": @"start"};
    NSDictionary *dict2 = @{@"name2": @"张三", @"status2": @"end"};
    

}

- (void)setUpObjc_msgSend
{
    /*
     objc_msgSend(id  _Nullable self, SEL  _Nonnull op, ...)
     :谁发送消息
     :发送什么消息
     */
    
    /*
     //在项目配置文件 -> Build Settings -> Enable Strict Checking of objc_msgSend Calls 这个字段设置为 NO, 默认为YES.
     
    PeopleModel *man = objc_msgSend(objc_getClass("PeopleModel"),sel_registerName("alloc"));
    man = objc_msgSend(man, sel_registerName("init"));
    objc_msgSend(man, @selector(eatFood:),@"dog");
    objc_msgSend(man, @selector(getHobby));
    objc_msgSend(man, @selector(drink:),@"beer");
    */
    
    PeopleModel *man = [[PeopleModel alloc] init];
    [man eatFood:@"meat"];
    [man getHobby];
    [man drink:@"beer"];
    NSLog(@"%@",man.address);
    [PeopleModel getClassInfo];
    
    
    NSLog(@"man =========== %@",man);

    
    size_t size = class_getInstanceSize(man.class);
    
    NSLog(@"class_getInstanceSize ------ %zu",size);
    
    /*
      class_copyPropertyList返回的仅仅是对象类的属性(@property申明的属性)
      class_copyIvarList返回类的所有属性和变量(包括在@interface大括号中声明的变量)
     */
    unsigned int count = 0;//成员变量的个数
    Ivar *ivarLsit = class_copyIvarList(man.class, &count);
    NSLog(@"成员变量的个数 ------- %d",count);
    for (int i = 0; i < count; i ++)
    {
        Ivar ivar1 = ivarLsit[i];
        
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar1)];
        NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar1)];
        NSLog(@"key == %@",key);
        
        NSLog(@"type == %@",type);

        
    }    
    free(ivarLsit);
}


@end
